﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KqAutoDesign.Base;

namespace KqAutoDesign.Models
{
    public class GlobalConfigs
    {
        public GlobalConfigs() 
        {
            IsConfigLoaded = false;
        }
        public string IconFolderPath { get; set; }
        public string ResourceFolderPath {  get; set; }
        /// <summary>
        /// 工作流目录，末尾带双斜杠
        /// </summary>
        public string WorkflowFolderPath { get; set; }
        public string ConfigFilePath { get; set; }
        public bool IsConfigLoaded { get; set; }
        public ObservableCollection<RegistedApp> Apps { get; set; }=new ObservableCollection<RegistedApp>();
        public ObservableCollection<WorkFlowBase> WorkFlowTemplates { get; set; }= new ObservableCollection<WorkFlowBase>();
        public Resources Resources { get; set; }

    }

    public class GlobalData
    {
        private static GlobalGroupParams _glbParams;
        /// <summary>
        /// 全局参数字典
        /// </summary>
        public static GlobalGroupParams GlobalParams 
        {
            get { return _glbParams; }
            set { _glbParams = value; }
        }

        /// <summary>
        /// 全局项目数据
        /// </summary>
        public static List<ProjectInfo> Projects { get; set; } = new List<ProjectInfo>();

        /// <summary>
        /// 获取全局字典数据
        /// </summary>
        public static void InitGlobalDictionary()
        {
            if(GlobalParams==null)
                GlobalParams = new GlobalGroupParams();
            else
                GlobalParams.Clear();
            if (App.GlobalConfigs.Apps!=null)
            {
                foreach (var app in App.GlobalConfigs.Apps)
                {
                    GlobalParams.AddRange(app.GetGlobalParams());
                }
                foreach (var prj in Projects)
                {
                    GlobalParams.AddRange(prj.GetGlobalParams());
                }
                foreach (var wf in App.GlobalConfigs.WorkFlowTemplates)
                {
                    var wfgpms = wf.GetGlobalParams();
                    GlobalParams.AddRange(wfgpms);
                }
            }
        }
    }

    public class Resources
    {
        public ObservableCollection<IconInfo> Icons { get; set; } = new ObservableCollection<IconInfo>();
    }



    public class IconInfo:NotifyBase
    {
        public IconInfo(string path)
        {
            IconPath = path;
        }
        private string _path;
        public string IconPath
        {
            get { return _path; } 
            set {
                if (_path != value) 
                { 
                    _path = value;
                    if (string.IsNullOrEmpty(_path))
                    {
                        Image = null;
                        FileName = null;
                    }
                    else
                    {
                        Image = new BitmapImage(new Uri(_path, UriKind.Absolute));
                        FileName = System.IO.Path.GetFileName(_path);
                    }
                    OnPropertyChanged("Path");
                }
            }
        }

        private string _fileName;
        public string FileName
        {
            get { return _fileName; }
            set
            {
                if (_fileName != value)
                {
                    _fileName = value;
                    OnPropertyChanged("Path");
                }
            }
        }

        private BitmapImage _image;
        public BitmapImage Image
        {
            get { return _image; }
            set
            {
                if (_image != value)
                {
                    _image = value;
                    OnPropertyChanged("Image");
                }
            }
        }
    }
}
